Thread: readnum example from jumping into c++

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    readnum example from jumping into c++

    Hi,

    I have started out c++ with jumping into c++ and in chapter 3 there is an example readnum.cpp which i have modified to get another input as float variable as below
    Code:
    #include <iostream>
    int main()
    {
        int thisisanumber;
        float thisisanothernumber;
        cout << "Please enter a number: ";
        cin >> thisisanumber;
        cout << "You have entered: " << thisisanumber << endl;
        cout << "Please enter another number: ";
        cin >> thisisanothernumber;
        cout << "You have entered: " << thisisanothernumber << endl;
    }
    Now if i give a int input to first number everything works fine. But if i give a decimal input to the first variable i expected it to truncate and just show a int, but strange thing happens which is it skips the second input and directly displays me both the output
    for e.g.
    Please enter a number: 32.5
    You have entered: 32
    Please enter another number: You have entered: 0.5

    What would be the cause?

    Thanks a lot in advance.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You've just entered the wonderful world of input validation. Basically, unless your input is perfect you cannot read it in so simply. Often in beginner programs you just assume the input is perfect to not get bogged down in details.

    What's happening is that the input function is scanning the input looking for an int. It reads character by character and as soon as it finds a character that doesn't belong in an int (such as a space, newline, period, basically any non-digit) it stops reading at that point. Any further input operations begin at that point so you can see why your second input got the fractional part of your first number.

    In general, if your input is line-based then it's best to read a line into a string and then parse the string for the expected data.

    But your specific problem can be solved by simply reading the first number into a float and then transferring it to an int (if you feel that's necessary) while rounding it up manually by adding 0.5, something like:
    Code:
    float x;
    cin >> x;
    int n = int(x + 0.5);
    BTW, it's common to use doubles unless there's a reason to use floats (hardware/software constraints, size/performance issues), since doubles have more precision.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    That helps - thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. jumping into c++ ch 11
    By jvar707 in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2015, 12:30 AM
  2. after 'jumping to c++'
    By nish1013 in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2013, 08:26 AM
  3. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2013, 08:29 PM
  4. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2013, 09:45 AM
  5. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM